var fs = require('fs');
const express = require(`express`)
const asyncHandler = require('express-async-handler');
const multer  = require('multer');

const router = express.Router()
var upload = multer({dest: 'uploads/'});

router.post('/', upload.any(), asyncHandler(uploadFile));
// router.post('/', upload.any(), function(req, res, next) {
async function uploadFile(req, res){
    console.log(req.files[0]);  // 上传的文件信息

    var des_file = "./uploads/" + req.files[0].originalname;
    fs.readFile( req.files[0].path, function (err, data) {
        fs.writeFile(des_file, data, function (err) {
            if( err ){
                // console.log( err );
                res.json({code: -1, message:'文件上传失败:' + err})
            }else{
                res.json({code: 20000, message:'文件上传成功', data:req.files[0].originalname})
            }
        });
    });
}

module.exports = router